
Skt.UsersManager = L.Class.extend({
	
	includes: L.Mixin.Events,
	
	events: ['usersUpdate','requestsUpdate'],

	initialize: function(sketchpad) {
		this._sketchpad = sketchpad;
		
		this._users = {};
		this._requests = {};
		
		this._couple(this._sketchpad.getOptions().coupling);
	},
	
	refreshUsers: function() {
		var self = this;
		
		$.getJSON( self._sketchpad.getBasePath() + "/subjectMembers/" + self._sketchpad.getSubjectVersion().id + ".json",
			function( data ) {
				for ( var id in data )
					self._registerUser(data[id]);
				
				self.fire('usersUpdate', {
					action: 'refreshUsers',
					users: self._users,
					requests: self._requests,
					usersManager: self
				});
			}
		);
		
	},
	
	_registerUser: function(user) {
		var self = this;
		var id = user.id;
		var regUser = self.getUser(id);
		
		regUser.username = user.username;
		regUser.name = user.name;
		regUser.lastname = user.lastname;
		regUser.type = user.type;
		regUser.mail = user.mail;
	},
	
	addInstance: function(userId) {
		var self = this;
		var user = self.getUser(userId);
		user.instances = user.instances + 1;
		
		self.fire('usersUpdate', {
			userId: userId,
			action: 'addInstance',
			users: self._users,
			requests: self._requests,
			usersManager: self
		});
	},
	
	removeInstance: function(userId) {
		var self = this;
		var user = self.getUser(userId);
		user.instances = user.instances - 1;
		
		self.fire('usersUpdate', {
			userId: userId,
			action: 'removeInstance',
			users: self._users,
			requests: self._requests,
			usersManager: self
		});
	},

	issueEditionRequest: function(userId, roomId) {
		var self = Skt.UsersManager.findSelf();
		
		console.log("self.fire('requestsUpdate', {");
		self.fire('requestsUpdate', {
			userId: userId,
			roomId: roomId,
			action: 'issueEditionRequest',
			users: self._users,
			usersManager: self
		});
	},
	
	dismissEditionRequest: function(userId, roomId) {
		var self = Skt.UsersManager.findSelf();
		
		self.fire('requestsUpdate', {
			userId: userId,
			roomId: roomId,
			action: 'dismissEditionRequest',
			users: self._users,
			usersManager: self
		});
	},
	
	isRequesting: function(userId) {
		var self = this;
		return self.getRequestsByUser(userId).length > 0;
	},
	
	isThereAnyRequester: function() {
		var self = this;
		for (var id in self._users)  {
			if ( self.getRequestsByUser(id).length > 0 )
				return true;
		}
		
		return false;
	},
	
	isUserOnline: function(userId) {
		var self = this;
		var user = self.getUser(userId);
		return user.instances > 0;
	},
	
	getSketchpad: function() {
		var self = this;
		return self._sketchpad;
	},

	getUsers: function() {
		var self = this;
		return self._users;
	},
	
	getUser: function(userId) {
		var self = this;
		if ( !self._users.hasOwnProperty(userId)  )
			self._users[userId] = { id: userId, instances: 0 };
		return self._users[userId];
	},
	
	getUserCount: function() {
		var self = this;
		var res = 0;
		for (var id in self._users)  {
			if ( self._users[id].hasOwnProperty('name') )
				res++;
		}
		return res;
	},
	
	getRequests: function() {
		var self = this;
		return self._requests;
	},
	
//	getState: function() {
//		var self = this;
//		return {
//			users: self._users,
//			requests: self._requests
//		};
//	},
//	
//	setState: function(state) {
//		var self = this;
//		self._users = state.users;
//		self._requests = state.requests;
//	},
	
	_couple: function(coupling) {
		if ( coupling.enabled ) {
			coupling.activityAdapter.coupleObject(
				"usersmanager",
				this,
				{
					messageType: "EVENT",
					explicitMapping: ["addInstance", "removeInstance","issueEditionRequest","dismissEditionRequest"]
				}
			);
		}
	}

});

Skt.UsersManager.findSelf = function() {
	return Skt.sketchpad.getUsersManager();
};
